﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Hypermarket_Serial_Port
{
    class CancelAllTransactionManager : CashierRegisterTaskManager
    {
        
        #region Constructor
        public CancelAllTransactionManager()
        {

        }
        #endregion

        #region Operation for cancel all transactions
        public override List<string> TaskOperation(List<string> receivedInfoList, Dictionary<string, CashierRegister> cashierDict)
        {
            try
            {
                SerialPortLog.Log("Cancel All Transactions Start");
                CashierRegister cashierRegister;
                string machineID = receivedInfoList[Constant.ZERO];
                SerialPortLog.LogOperation("Cancel All Transaction: " + machineID);
                string type = receivedInfoList[Constant.ONE];

                if (cashierDict.TryGetValue(machineID, out cashierRegister))
                {
                    return CancelAllTransaction(cashierRegister, machineID,type);
                }
                else
                {
                    //return Constant.LOGIN_FIRST_MSG + Constant.NEWLINE;
                    List<string> output = new List<string>();
                    output.Add(machineID);
                    output.Add(type);
                    output.Add(Constant.FAIL);
                    SerialPortLog.LogOperation("Cancel All Transactions Fail");
                    SerialPortLog.Log("Cancel All Transactions Fail");
                    return output;
                }

            }
            catch
            {
                SerialPortLog.LogOperation("Cancel All Transactions Error");
                SerialPortLog.Log("Cancel All Transactions Error");
                throw;
            }
        }

        private List<string> CancelAllTransaction(CashierRegister cashierRegister, string machineID, string type)
        {
            if (cashierRegister.isTransactionStarted())
            {
                cashierRegister.CancelAllTransactions();
                //return Constant.CANCEL_ALL_TRANSACTIONS_MSG + Constant.NEWLINE;
                List<string> output = new List<string>();
                output.Add(machineID);
                output.Add(type);
                output.Add(Constant.SUCCESS);
                SerialPortLog.LogOperation("Cancel All Transactions Sucessfully");
                SerialPortLog.Log("Cancel All Transactions Sucessfully");
                return output;
            }
            else
            {
                List<string> output = new List<string>();
                output.Add(machineID);
                output.Add(type);
                output.Add(Constant.FAIL);
                SerialPortLog.Log("Cancel All Transactions Fail");
                SerialPortLog.Log("Cancel All Transactions Fail");
                return output;
                //return Constant.START_TRANSACTION_FIRST + Constant.NEWLINE;
            }
        }


        #endregion

    }
}
